﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class SubscribesDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        //根据分组获取用户
        public DataSet getSubs(int groupid)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@groupid", groupid)
            };
            string sqlstr = "select * from subscribes where groupid = @groupid";
            if (groupid == 1001)
            {
                sqlstr = sqlstr + " or 1 = 1";
            }
            return SqlHelper.ExecuteDataSet(conn,CommandType.Text,sqlstr,sp);
        }
        //subscribe,openid, nickname, sex, language, city, province, country, headimgurl,subscribe_time, unionid, remark, groupid
        /// <summary>
        /// 插入关注用户信息
        /// </summary>
        /// <param name="subscribe">是否关注</param>
        /// <param name="openid">用户openid</param>
        /// <param name="nickname">用户昵称</param>
        /// <param name="sex">性别</param>
        /// <param name="language">语言</param>
        /// <param name="city">城市</param>
        /// <param name="province">省，州</param>
        /// <param name="country">国家</param>
        /// <param name="headimgurl">头像url</param>
        /// <param name="subscribe_time">关注时间</param>
        /// <param name="unionid">统一id</param>
        /// <param name="remark">备注</param>
        /// <param name="groupid">分组号</param>
        public int addSubs(int subscribe,string openid,string nickname,string sex,string language,string city,string province,string country,string headimgurl,string subscribe_time,string unionid,string remark,int groupid)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@subscribe",subscribe),
                new SqlParameter("@openid",openid),
                new SqlParameter("@nickname",nickname),
                new SqlParameter("@language",language),
                new SqlParameter("@sex",sex),
                new SqlParameter("@city",city),
                new SqlParameter("@province",province),
                new SqlParameter("@country",country),
                new SqlParameter("@headimgurl",headimgurl),
                new SqlParameter("@subscribe_time",subscribe_time),
                new SqlParameter("@unionid",unionid),
                new SqlParameter("@remark",remark),
                new SqlParameter("@groupid",groupid)
            };
            string sqlstr = "insert into subscribes values(@subscribe,@openid, @nickname, @sex, @language, @city, @province, @country, @headimgurl, @subscribe_time, @unionid, @remark, @groupid)";
            return SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlstr,sp);
        }
        //删除取消关注用户
        public int delSubs(string openid)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@openid",openid)    
            };
            string sqlstr = "delete from subscribes where openid = @openid";
            int i = SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlstr,sp);
            return i;
        }
        //根据用户名称查找关注用户
        public DataSet getSubsUser(string nickname)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter("@nickname","%" + nickname + "%")
            };
            string sqlstr = "select * from subscribes where nickname like @nickname";
            return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sqlstr, sp);
        }
        //批量插入用户
        public void synchroSubs(string tablename,DataTable dt)
        {
            SqlHelper.SqlBulkCopyInsert(conn, tablename,dt);
        }
    }
}
